package com.itany.springcloud.springcloudprovider.controller;
import com.itany.springcloud.springcloudcommon.entity.User;
import com.itany.springcloud.springcloudcommon.vo.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "用户服务相关的API接口")
@RestController
@RequestMapping("/users")
public class UserController {

    private static Map<Integer, User> users=new HashMap<Integer,User>();
    private static Integer id=1;


    @ApiOperation("添加用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名"),
            @ApiImplicitParam(name = "password",value = "密码")
    })
    @PostMapping
    public ResponseResult add(User user){
        System.out.println("provider.UserController.add()");
        user.setId(id++);
        users.put(user.getId(),user);
        return ResponseResult.success();
    }

    @ApiOperation("查询所有用户")
    @GetMapping
    public ResponseResult findAll(){
        System.out.println("provider.UserController.findAll()");
        List<User> list=new ArrayList<User>(users.values());
        return ResponseResult.success(list);
    }

    @ApiOperation("根据id查询一个用户")
    @ApiImplicitParam(name="id",value = "用户编号",dataType = "Integer")
    @GetMapping("/{id}")
    public ResponseResult findById(@PathVariable("id") Integer id){
        System.out.println("provider.UserController.findById()");
        return ResponseResult.success(users.get(id));
    }

    @ApiOperation("根据id删除一个用户")
    @ApiImplicitParam(name="id",value = "用户编号",dataType = "Integer")
    @DeleteMapping("/{id}")
    public ResponseResult remove(@PathVariable("id") Integer id){
        System.out.println("provider.UserController.remove()");
        users.remove(id);
        return ResponseResult.success();
    }

    @ApiOperation("修改用户")
    @PutMapping
    public ResponseResult modify(User user){
        System.out.println("provider.UserController.modify()");
        users.put(user.getId(),user);
        return ResponseResult.success();
    }

}
